# ======================================================================
# Define options
# ======================================================================
set val(chan)           Channel/WirelessChannel    ;# channel type
set val(nc)		   5				;#number of channels
set val(ni)		   2				  ;# number of interface	
set val(prop)           Propagation/TwoRayGround   ;# radio-propagation model
set val(netif)          Phy/WirelessPhy            ;# network interface type
set val(mac)            Mac/802_11x                 ;# MAC type
set val(ifq)            Queue/DropTail/PriQueue    ;# interface queue type
set val(ll)             LL                         ;# link layer type
set val(ant)            Antenna/OmniAntenna        ;# antenna model
set val(ifqlen)         80                         ;# max packet in ifq
set val(nn)             5                          ;# number of mobilenodes
set val(rp)             NXrouting                  ;# routing protocol

#may use any of these 3
#$val(mac) set RateShape_ 1.0e6 ;#1mbps
#$mac_($i) set dataRate_ 54Mb	;#dataRate_
#$mac_($i) set basicRate_ 24Mb	;#basicRate_ seems to be for the broadcast

set ns [new Simulator]

set nd     [open out.tr w]
$ns trace-all $nd

set nf [open out.nam w]
$ns namtrace-all $nf

create-god [expr [expr $val(nn)-1] * [expr $val(ni)+1]]

set topo       [new Topography]

$topo load_flatgrid 500 500

#creation of wireless channels
#The last channel chan($val(nc)-1) for WLAN
#channel from 0 to $val(nc)-2 for WWAN

for {set i 0} {$i < $val(nc)} {incr i} {
	set chan_($i) [new $val(chan)]
}



$ns node-config -adhocRouting $val(rp) \
			 -ifNum  $val(ni) \
			 -llType $val(ll) \
			 -macType $val(mac) \
			 -ifqType $val(ifq) \
			 -ifqLen $val(ifqlen) \
			 -antType $val(ant) \
			 -propType $val(prop) \
			 -phyType $val(netif) \
			 -channel $chan_(0) \
			 -topoInstance $topo \
			 -agentTrace ON \
			 -routerTrace ON \
			 -macTrace OFF \
			 -movementTrace OFF	

#set mobile node
#mobile node for node 0 to node nn-2 ;that is node 0 1 2 3
for {set i 0} {$i < [expr $val(nn)-1] } {incr i} {			 
	$ns change-numifs 2
	$ns add-channel 0 $chan_($i)
	$ns add-channel 1 $chan_([expr $val(nc)-1])
	
	set node_($i) [$ns node]
	$node_($i) set X_ 100
	$node_($i) set Y_ [expr 100 + $i]
	$node_($i) shape-rate 0 1Mb
	$node_($i) shape-rate 1 54Mb
	
} 

#set BS node
#BS node ,node 4
$ns change-numifs [expr $val(nc)-1]
for {set i 0} {$i < [expr $val(nc) -1] } {incr i} {
	$ns add-channel $i $chan_($i)
}
set bs_node [$ns node]
$bs_node set X_ 100
$bs_node set Y_ 100
for {set i 0} {$i < [expr $val(nc) -1] } {incr i} {
	$bs_node shape-rate $i 1Mb
}

			 
proc finish { } {
	global ns nd nf 
	$ns flush-trace
	close $nf
	close $nd
	puts "Simulation Ended ..."
	exit 0
}


set udp0 [new Agent/UDP/UDP2SP]
$udp0 set packetSize_ 1500
$ns attach-agent $bs_node $udp0
set p2spSrvr [new Application/p2sp/p2spSrvr]
$p2spSrvr set chunksize_ 5000
$p2spSrvr set filesize_ 5000000
$p2spSrvr attach-agent $udp0

$ns at 0.5 "$p2spSrvr start"
$ns at 300.0 "$p2spSrvr stop"

for {set i 0} {$i < [expr $val(nn)-1]} {incr i} {
	set udp_($i) [new Agent/UDP/UDP2SP]
	$udp_($i) set packetSize_ 1500
	$ns attach-agent $node_($i) $udp_($i)
	set p2spC_($i) [new Application/p2sp/p2spClient]
	$p2spC_($i) set p2sp_enable_ 1
	$p2spC_($i) set chunksize_ 5000
	$p2spC_($i) set filesize_ 5000000
	$p2spC_($i) set group_size_ [expr $val(nn)-1]
	$p2spC_($i) set wwan_rate_ 1Mb
	$p2spC_($i) set wlan_rate_ 54Mb
	
	$p2spC_($i) set peer_req_jitter_ 0.01
	$p2spC_($i) set peer_data_jitter_ 0.05
	$p2spC_($i) set srvr_data_timeout_ 2
	$p2spC_($i) set peer_data_timeout_ 3
	
	$p2spC_($i) attach-agent $udp_($i)
	$ns connect $udp_($i) $udp0
	$ns at 0.6 "$p2spC_($i) start"
	$ns at 300.0 "$p2spC_($i) stop"
	
}

$ns at 300.01 "finish"
puts "Starting Simulation..."
$ns run
